package com.are.odisea;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class GestionTipoReparacion {
	private db conexion = null;
	private TipoReparacion tiporepacion;
	
	public static final int FILTER_BY_DESCRIPTION = 1;
	public static final int FILTER_BY_CODIGO = 2;
	
	public db getConexion() {
		return conexion;
	}
	public void setConexion(db conexion) {
		this.conexion = conexion;
	}
	public TipoReparacion getTiporepacion() {
		return tiporepacion;
	}
	public void setTiporepacion(TipoReparacion tiporepacion) {
		this.tiporepacion = tiporepacion;
	}
	public GestionTipoReparacion(db conexion, TipoReparacion tiporepacion) {
		super();
		this.conexion = conexion;
		this.tiporepacion = tiporepacion;
	}
	public GestionTipoReparacion(db conexion) {
		super();
		this.conexion = conexion;
	}
	
	public ArrayList<TipoReparacion> List() throws SQLException {
		ArrayList<TipoReparacion> lista = new ArrayList<TipoReparacion>();
		String sql = "select tirecodi,tiredesc,tireacti from tipo_reparacion order by tireacti,tiredesc";
		java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
		ResultSet rs = conexion.Query(pst);
		while(rs.next()) {
			TipoReparacion tr = new TipoReparacion();
			tr.setCodigo(rs.getString("tirecodi"));
			tr.setDescripcion(rs.getString("tiredesc"));
			tr.setProducto(rs.getString("tireacti"));
			lista.add(tr);
		}
		
		return lista;
	}
	public ArrayList<TipoReparacion> ListByProduct(String producto) throws SQLException {
		ArrayList<TipoReparacion> lista = new ArrayList<TipoReparacion>();
		String sql = "select tirecodi,tiredesc,tireacti from tipo_reparacion where tireacti=? order by tireacti,tiredesc";
		java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
		pst.setString(1, producto);
		ResultSet rs = conexion.Query(pst);
		while(rs.next()) {
			TipoReparacion tr = new TipoReparacion();
			tr.setCodigo(rs.getString("tirecodi"));
			tr.setDescripcion(rs.getString("tiredesc"));
			tr.setProducto(rs.getString("tireacti"));
			lista.add(tr);
		}
		
		return lista;
	}
	
	public ArrayList<TipoReparacion> Filter(String criterio, String producto, int filter) throws SQLException {
		ArrayList<TipoReparacion> lista = new ArrayList<TipoReparacion>();
		String sql = "";
		switch (filter) {
		case FILTER_BY_DESCRIPTION:
			sql = "select tirecodi,tiredesc,tireacti from tipo_reparacion where tireacti=? and tiredesc like ? order by tireacti,tiredesc";
			break;
		case FILTER_BY_CODIGO:
			sql = "select tirecodi,tiredesc,tireacti from tipo_reparacion where tireacti=? and tirecodi like ? order by tireacti,tiredesc";
			break;
		default:  // por defecto filtro por descripcion
			sql = "select tirecodi,tiredesc,tireacti from tipo_reparacion where tireacti=? and tiredesc like ? order by tireacti,tiredesc";
		}
		
		java.sql.PreparedStatement pst = conexion.getConnection().prepareStatement(sql);
		pst.setString(1, producto);
		pst.setString(2, "%" + criterio + "%");
		ResultSet rs = conexion.Query(pst);
		while(rs.next()) {
			TipoReparacion tr = new TipoReparacion();
			tr.setCodigo(rs.getString("tirecodi"));
			tr.setDescripcion(rs.getString("tiredesc"));
			tr.setProducto(rs.getString("tireacti"));
			lista.add(tr);
		}
		
		return lista;
	}

}
